/**============================================================================
 Confidential Information - Limited distribution to authorized persons only.
 This software is protected as an unpublished work under the U.S. Copyright
 Act of 1976.

 Copyright (c) 2012, BluePhoenix Solutions, Inc. All rights reserved.
 ----------------------------------------------------------------------------
 file......: VariantsListAdapter.java
 Author....: Vyacheslav Kovalyov
 Created...: 1/23/12

 Description:

 ============================================================================*/
package com.bphx.android.activities.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.bphx.android.activities.TrainingActivity;
import com.bphx.android.words.Word;
import org.apache.commons.lang.ObjectUtils;

import java.util.List;

import static android.content.Context.LAYOUT_INFLATER_SERVICE;
import static com.bphx.android.R.id.playButton;
import static com.bphx.android.R.id.text;
import static com.bphx.android.R.layout.training_word;
import static com.bphx.android.utils.Utils.getTextFrom;

/**
 * @author Vyacheslav Kovalyov
 * @version 1.0
 */
public class VariantsListAdapter extends BaseAdapter {

    public VariantsListAdapter(TrainingActivity parentActivity, Word word, List<String> items) {

        this.word = word;
        this.items = items;
        this.parentActivity = parentActivity;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return getItem(position).hashCode();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) parentActivity.getSystemService(LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(training_word, parent, false);

        final TextView tw = (TextView) view.findViewById(text);
        tw.setText(ObjectUtils.toString(getItem(position)));

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (word.getVariants().contains(getTextFrom(tw))) {
                    parentActivity.dispatchRightAnswer();
                } else {
                    parentActivity.dispatchWrongAnswer();
                }
            }
        });

        view.findViewById(playButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                parentActivity.speech(tw.getText().toString());
            }
        });
        return view;
    }

    private final TrainingActivity parentActivity;
    private final List<String> items;
    private final Word word;
}
